const { ccclass, property } = cc._decorator;

import EventMgr from "./EventMgr";
import MatchGameEventType from "./MatchGameEventType";
import frameworkManager from "./frameworkManager";
import NodePool from "./NodePool";
import TargetAniCtrl from "./TargetAniCtrl";
import MatchItemPrefabCtrl from "./MatchItemPrefabCtrl";
import MatchGameViewCtrl from "./MatchGameViewCtrl";
import MatchGameData from "./MatchGameData";
import MatchGameConfig from "./MatchGameConfig";
import MatchGameDataMgr from "./MatchGameDataMgr";
import ObstacleCtrl from "./ObstacleCtrl";

import i from "./EventMgr"
 import r from "./MatchGameEventType"
 import n from "./frameworkManager"
 import l from "./NodePool"
 import s from "./TargetAniCtrl"
 import c from "./MatchItemPrefabCtrl"
 import u from "./MatchGameViewCtrl"
 import p from "./MatchGameData"
 import d from "./MatchGameConfig"
 import h from "./MatchGameDataMgr"
 import f from "./ObstacleCtrl"

@ccclass
class MatchItemCtrl {
    matchItem = null;
    matchItemParent = null;
    matchItemMaskParent = null;
    matchClearArr = [];
    isReady = false;

    static _instance = null;

    static getInstance():MatchItemCtrl {
        if (!this._instance) {
            this._instance = new MatchItemCtrl();
        }
        return this._instance;
    }

    ready(e, t, a?) {
        this.matchItem = e;
        this.matchItemMaskParent = a;
        NodePool.initPool(this.matchItem, 50, MatchGameConfig.SquarePool.MatchItem);
        this.matchItemParent = t;
        this.init();
        this.isReady = true;
    };

    init() {
        this.initMatchClearArr();
    };

    initMatchClearArr() {
        this.matchClearArr = [];
        for (let e = 0; e < MatchGameConfig.MAX_MATCH_TYPE_COUNT; e++) {
            this.matchClearArr.push({
                matchType: e + 1,
                count: 0
            });
        }
    };

    pushMatchClearArr(e, t = 1) {
        void 0 === t && (t = 1);
        for (var a = e.itemType, o = 0; o < this.matchClearArr.length; o++) if (this.matchClearArr[o] && this.matchClearArr[o].matchType == a) {
            this.matchClearArr[o].count += t;
            break;
        }
        var n = p.isTargetType(d.SquareType.MatchItem, a);
        if (n) {
            var l = s.playMatchFlyAni(e.node.position, u.instance.targetItems[n.index], a, function () {
                i.trigger(r.FLY_ONE_TARGET, {
                    index: n.index
                });
            });
            p.delatyUpdateOnsTargetCount(l, d.SquareType.MatchItem, this.matchClearArr[o].matchType);
        }
    };

    setTarget() { };

    createMatchItem(e, t, a, o = false) {
        let i = NodePool.getNode(MatchGameConfig.SquarePool.MatchItem);
        i.parent = o ? this.matchItemMaskParent : this.matchItemParent;
        if (!i.getComponent(MatchItemPrefabCtrl)) {
            i.addComponent(MatchItemPrefabCtrl);
        }
        let r = i.getComponent(MatchItemPrefabCtrl);
        r.init(e, t, a);
        return r;
    };

    createMatchItemNoPurpleGem(e, t, a, o = false) {
        let i = NodePool.getNode(MatchGameConfig.SquarePool.MatchItem);
        i.parent = o ? this.matchItemMaskParent : this.matchItemParent;
        if (!i.getComponent(MatchItemPrefabCtrl)) {
            i.addComponent(MatchItemPrefabCtrl);
        }
        let r = i.getComponent(MatchItemPrefabCtrl);
        r.init(e, t, a);
        return r;
    };

    destoryMatchItem(e, t = false) {
        let a = e.getComponent(MatchItemPrefabCtrl);
        if (a) {
            this.pushMatchClearArr(a, 1);
            a.destorySelf(false).then(function () {
                ObstacleCtrl.onMatchClear(a);
                NodePool.putNode(MatchGameConfig.SquarePool.MatchItem, e);
            });
        } else {
            frameworkManager.log("销毁的不是棋子", e);
        }
    };

    destoryMatchItems(e) {
        e.forEach( (e) => {
            let a = e.getComponent(MatchItemPrefabCtrl);
            if (a) {
                this.pushMatchClearArr(a, 1);
                a.destorySelf(true).then(function () {
                    ObstacleCtrl.onMatchClear(a);
                    NodePool.putNode(MatchGameConfig.SquarePool.MatchItem, e);
                });
            } else {
                e.removeFromParent();
                frameworkManager.log("销毁的不是棋子", e);
            }
        });
    };

    getMatchItemsByType(e) {
        let t = [];
        let a = MatchGameDataMgr.middleSquareGrid;
        for (let o = 0; o < MatchGameConfig.MAX_ROW; o++) {
            for (let i = 0; i < MatchGameConfig.MAX_COL; i++) {
                if (a[o][i] && a[o][i].squareType == MatchGameConfig.SquareType.MatchItem && a[o][i].itemType == e) {
                    t.push(a[o][i]);
                }
            }
        }
        return t;
    };
}

export default MatchItemCtrl.getInstance();